using("Popup");
PopupButton.inherit("Control");

function PopupButton(node){
    this.Control(node);
    this.init(this.properties.getObject("config"));
}

/*
function PopupButton(config){
	if (arguments.length>0){
		this.init(config);									
	}
} 
*/


PopupButton.prototype.init=function (config) {
	this.config=config;
	this.buttonForm=document.getElementById(config["buttonId"]);
	this.hiddenCollection=new HiddenCollection(document.getElementById(config["hiddenContainerId"]), config["uniqueId"]);
	//this.eventListener=new PopupButtonEventListener(this);
	this.eventManager=new EventManager();
	this.popup=config["popup"];
}


PopupButton.prototype.getEventManager = function () {
    return this.eventManager;
}

PopupButton.prototype.onprepopulate=function (eventInfo) {
	var buttonEventInfo={"data": eventInfo.data, "target": this, "cancel": false};
	this.eventManager.fireEvent("prepopulate", buttonEventInfo);
	eventInfo.cancel=buttonEventInfo.cancel;
}

PopupButton.prototype.onpopulate=function (eventInfo) {
	var buttonEventInfo={"data": eventInfo.data, "target": this, "cancel": false};
	this.eventManager.fireEvent("populate", buttonEventInfo);
	if (Utils.isDefined(eventInfo.data)&&!buttonEventInfo.cancel){
	    this.populate(eventInfo.data);
	}
	this.processPostBack();
}


PopupButton.prototype.populate=function (results) {
    this.populateValues(results);
	for (var i=0; i<this.displayControls.getCount(); i++){
		this.displayControls.getValueByIndex(i).populate(this.config["displayControls"].keys[i], results);	
	}
}

PopupButton.prototype.populateValues = function (results) {
    var values = new Array();
    for (var i=0; i<this.config["valueKeys"].length; i++){
        values.push(results.getValue(this.config["valueKeys"][i]));    
    }
    this.setValues(values);
}

PopupButton.prototype.getValuesState = function (map) {
    var values = this.getValues();
    for (var i=0; i<this.config["valueKeys"].length; i++){
        map.setValue(this.config["valueKeys"][i], values[i]);
    }
}

PopupButton.prototype.processPostBack=function (doPostBack) {
	if (this.config["doPostBack"]){
		//Not in use
		//this.eventManager.fireEvent("postback", {"target": this});
		eval(this.config["postBackScript"]);
	}
}

PopupButton.prototype.toString=function () {
	return "";	
}

//Clears state of display controls. Called when parameter is changed.
PopupButton.prototype.clearAll=function () {
	if (this.hiddenCollection.getCount()>0){
		for (var i=0; i<this.displayControls.getCount(); i++){
			if (this.displayControls.getValueByIndex(i)&&this.displayControls.getValueByIndex(i).clear){
				this.displayControls.getValueByIndex(i).clear();
			}
		}	
	}
	this.clearValues();
}

// Value methods ---------------------------------
PopupButton.prototype.clearValues=function () {
	this.hiddenCollection.clear();
	this.hiddenCollection.add("");
}

PopupButton.prototype.getValues = function () {
    var values = new Array();
    for (var i=0; i<this.hiddenCollection.getCount(); i++) {
        values.push(this.hiddenCollection.getValueByIndex(i));
    }
    return values;
}

PopupButton.prototype.setValues = function (values) {
    this.clearValues();
    
    if (values instanceof Map){
        values = values.values;
    }
    if (!(values instanceof Array)){
        var arr = new Array();
        arr.push(values);
        values = arr;
    }
    
    for (var i =0; i<values.length; i++) {
        this.hiddenCollection.setValueByIndex(i, values[i]);
    }
}
//---------------------------------------------------

//Onclick event handler
PopupButton.prototype.onclick=function (){
	if (!this.loaded) {
	    this.clickOnLoad = true;
	    return;
	}
	
	var params=this.getParams();
	var eventArgs = {"target": this, "params": params};
	this.eventManager.fireEvent("popup", eventArgs);
	this.popup.get(eventArgs.params);
}

//Gets state of all parameter controls
PopupButton.prototype.getParams=function () {
	var parameters=new Map(this.config["popupParameters"]);
	parameters.add("listener", this.config["controlVar"])
	for (var i=0; i<this.parameterControls.getCount(); i++) {
		var control=this.parameterControls.getValueByIndex(i);
		if (control&&control.getValue){
		    parameters.add(this.config["parameterControls"].keys[i], control.getValue());
		} 
	}
	return parameters;
}

//Gets state of all display controls & button internal state
PopupButton.prototype.getState=function () {
	var parameters=new Map();
	this.getValuesState(parameters);
	return parameters;
}

PopupButton.prototype.clear=function (){
	if (!this.buttonForm.disabled) {
		this.clearValues();
	}
}

PopupButton.prototype.findByNames=function (controls) {
	var resolved=new Array();
	for (var i=0; i<controls.length; i++){
		resolved.push(this.findByName(controls[i]));
	}
	return resolved;
}


PopupButton.prototype.setEnabled=function (enabled) {
	this.buttonForm.disabled = !enabled;
}


// Events
PopupButton.prototype.onload=function () {
	this.config["displayControls"].values=this.findByNames(this.config["displayControls"].values);
	this.config["parameterControls"].values=this.findByNames(this.config["parameterControls"].values);
	
	this.parameterControls=this.config["parameterControls"];
	this.displayControls=this.config["displayControls"];
	
	this.hookupRelatedEvents();
	this.loaded = true;
	if (this.clickOnLoad) {
	    this.onclick();
	}
}

PopupButton.prototype.hookupRelatedEvents=function () {
	if (this.config["hookupParameterControlsEvents"]) {
		//Event listener that reacts on parameters' changes
		var parameterEventListener={
			"button"		: this,
			"onchange"		: function () {this.button.clearAll();},
			"onkeypress"	: function () {this.button.clearAll();}
		}
	}
		
	//Event listener that reacts on related control state changes
	var relatedEventListener={
		"button"			: this,
		"onkeypress"		: function () {this.button.clearValues();},
		"onchange"			: function () {this.button.clearValues();}
	}
			
	if (this.config["hookupParameterControlsEvents"]) {
		for (var i=0; i<this.parameterControls.getCount(); i++) {
			if (this.parameterControls.values[i]&&this.parameterControls.values[i].getEventHandler){
				this.parameterControls.values[i].getEventHandler().registerListener("keypress", parameterEventListener);
				this.parameterControls.values[i].getEventHandler().registerListener("change", parameterEventListener);
			}		
		}
	}
	
	for (var i=0; i<this.displayControls.getCount(); i++){
		if (this.displayControls.values[i]&&this.displayControls.values[i].getEventHandler){
			this.displayControls.values[i].getEventHandler().registerListener("keypress", relatedEventListener);
			this.displayControls.values[i].getEventHandler().registerListener("change", relatedEventListener);
		}
	}	
}






